﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using com.fukefu.sql;

namespace com.fukefu.info
{
    public partial class PostForm : BaseForm
    {
        private static PostForm pf;
        private static Object obj = new Object();

        public static PostForm GetInstance()
        {
            if (pf == null || pf.IsDisposed)
            {
                lock (obj)
                {
                    if (pf == null || pf.IsDisposed)
                    {
                        pf = new PostForm();
                    }
                }
            }
            return pf;
        }

        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(PostForm));
        InformationFactory info = new InformationFactory();
        MenuItem _OneKeyToStartMenuItem = null;
        //数据库菜单
        SqlServerOperate sql = new SqlServerOperate();
        private PostForm()
        {
            InitializeComponent();
            //重写listboxDrowItem
            //绑定事件
            info.SetControl += SetControl;
            info._UpdateData += UpdateData;
            Menu_Init();
            new Thread(new ThreadStart(Pic)).Start();
        }
        private void Pic()
        {
            new MyChart(ref chart1).FlushData("select substring(CONVERT(varchar,dt,12),3,4),count(1) from 已发布 where DATEDIFF(D,dt,GETDATE())<7 group by CONVERT(varchar,dt,12)");
        }
        //菜单初始化
        private void Menu_Init()
        {
            MainMenu mainMenu = new MainMenu();
            //一级菜单
            SqlDataReader re = sql.Query("select * from 全局设置.dbo.信息平台分类 order by weights desc");
            Hashtable ht = new Hashtable();
            while (re.Read())
            {
                MenuItem item = new MenuItem(re["name"].ToString());
                mainMenu.MenuItems.Add(item);
                ht.Add(re["id"], item);
            }
            re.Close();
            //二级菜单
            re = sql.Query("select pid,title,class,domain from 全局设置.dbo.信息平台设置 where pid>0 order by weights desc");
            while (re.Read())
            {
                MenuItem item = ht[re["pid"]] as MenuItem;
                MenuItem _item = new MenuItem();
                _item.Text = re["title"].ToString();
                _item.Tag = re["title"].ToString();
                _item.Click += delegate
                {
                    info.Add(_item.Tag.ToString());
                    CreateTabPage(_item.Tag.ToString());
                };
                item.MenuItems.Add(_item);
            }
            re.Close();
            //一键启动
            MenuItem _start = new MenuItem("一键启动");
            mainMenu.MenuItems.Add(_start);
            //设置
            MenuItem __item = new MenuItem("设置");
            __item.Click += delegate
            {
                ShowForm(OneKeyToStartForm.GetInstance());
            };
            _start.MenuItems.Add(__item);
            //启动
            _OneKeyToStartMenuItem = new MenuItem("启动");
            _OneKeyToStartMenuItem.Click += delegate
            {
                _OneKeyToStartMenuItem.Enabled = false;
                OneKeyToStart();
            };
            _start.MenuItems.Add(_OneKeyToStartMenuItem);

            this.Menu = mainMenu;
        }
        //生成信息提示框
        private void CreateTabPage(string name)
        {
            //判断是否存在
            foreach (TabPage t in this.tabControl1.TabPages)
            {
                if (t.Text.Equals(name))
                {
                    return;
                }
            }
            //添加tab
            TabPage tab = new TabPage();
            tab.Parent = this.tabControl1;
            tab.Text = name;
            //添加listview
            Control listbox = new ListBox();
            listbox.Name = name;
            listbox.Location = new Point(3, 3);
            listbox.Size = new Size(this.tabControl1.Width - 14, this.tabControl1.Height - 31);
            listbox.Parent = tab;
        }
        //一键启动
        private void OneKeyToStart()
        {
            SqlServerOperate sql = new SqlServerOperate();
            SqlDataReader re = sql.Query("select * from [_site_一键启动] where title is not null and title!=''");
            List<string> list = new List<string>();
            while (re.Read())
            {
                list.Add(re["title"].ToString());
            }
            re.Close();
            sql.Close();
            foreach (string s in list)
            {
                info.Add(s);
                CreateTabPage(s);
            }
            _OneKeyToStartMenuItem.Enabled = true;
        }
        //验证码重绘边框
        private void flushcode_Paint(object sender, PaintEventArgs e)
        {
            Pen pp = new Pen(Color.Gray);
            e.Graphics.DrawRectangle(pp, e.ClipRectangle.X, e.ClipRectangle.Y, e.ClipRectangle.X + e.ClipRectangle.Width - 1, e.ClipRectangle.Y + e.ClipRectangle.Height - 1);
        }
        //开始发布输入验证码文章
        private void BeginButton_Click(object sender, EventArgs e)
        {
            info.vcode = GetControl("textbox1", ControlValue.Text).ToString();
            if (!info.vcode.Equals(""))
            {
                this.vcode.Image = ((System.Drawing.Image)(resources.GetObject("vcode.Image")));
                button1.Enabled = false;
            }
        }
        //窗体关闭
        private void PostForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            MenuItem[] item = this.MdiParent.Menu.MenuItems.Find("MessageMenuItem", false);
            if (item.Length > 0)
            {
                item[0].Visible = true;
            }
            item = this.MdiParent.Menu.MenuItems.Find("UpdateTitleMenuItem", true);
            if (item.Length > 0)
            {
                item[0].Enabled = true;
            }
        }
        //修改现在显示数据数据
        private void UpdateData(string value, object obj)
        {
            if (listView1.InvokeRequired)
            {
                listView1.BeginInvoke(new MyDelegate2_1(UpdateData), value, obj);
            }
            else
            {
                ListViewItem lvt = listView1.FindItemWithText(value);
                string[] str = obj as string[];
                if (lvt != null)
                {
                    lvt.SubItems[1].Text = str[0];
                    lvt.SubItems[2].Text = str[1];
                }
                else
                {
                    lvt = new ListViewItem(new string[] { value, str[0], str[1] });
                    this.listView1.Items.Add(lvt);
                }
            }
        }
        
    }
}
